﻿using System.IO;
using System.Text;
using System.Text.Json.Serialization;
using SMCL.Core.Utils.Auth;
using SMCL.Core.Utils.Static;

namespace SMCL.Core.Utils.Types
{
    public class LaunchInfo
    {
        [JsonInclude]
        public string GameArg { get; set; }
        [JsonInclude]
        public string JavaArg { get; set; }
        [JsonInclude]
        public int JavaVersion { get; set; }
        [JsonInclude]
        public string VersionName { get; set; }
        [JsonInclude]
        public string GameDir { get; set; }
        [JsonInclude]
        public string AssetsDir { get; set; }
        [JsonInclude]
        public string AssetsIndexName { get; set; }
        [JsonInclude]
        public string ClientId { get; set; }
        [JsonInclude]
        public string NativesDir { get; set; }
        [JsonInclude]
        public string ClassPathDir { get; set; }
        [JsonInclude]
        public string PlayerName { get; set; }
        [JsonInclude]
        public string AuthUuid { get; set; }
        [JsonInclude]
        public string AuthToken { get; set; }
        [JsonInclude]
        public string AuthXuid { get; set; }
        [JsonInclude]
        public string GameAddress { get; set; }
        [JsonInclude]
        public string ClassName { get; set; }
        [JsonIgnore]
        public string CommandLine
        {
            get
            {
                var sb = new StringBuilder();
                foreach (var i in GetAllFiles(ClassPathDir))
                    sb.Append($"{i.FullName};");
                sb.Append(GameAddress);
                return $"{JavaArg.Replace("${natives_directory}", NativesDir).Replace("${launcher_name}", Launcher.Brand).Replace("${launcher_version}", Launcher.Version.ToString()).Replace("${classpath}", sb.ToString())} {ClassName} {GameArg.Replace("${auth_player_name}", PlayerName).Replace("${version_name}", VersionName).Replace("${game_directory}", GameDir).Replace("${assets_root}", AssetsDir).Replace("${assets_index_name}", AssetsIndexName).Replace("${auth_access_token}", AuthToken).Replace("${clientid}", ClientId).Replace("${auth_xuid}", AuthXuid).Replace("${user_type}", "msa").Replace("${version_type}", "release").Replace("${auth_uuid}", AuthUuid)}";
            }
        }
        private static List<FileInfo> GetAllFiles(string directoryPath)
        {
            List<FileInfo> files = [];

            // 创建DirectoryInfo对象
            DirectoryInfo dir = new(directoryPath);

            // 检查目录是否存在
            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException($"Directory does not exist at path: {directoryPath}");
            }

            // 获取当前目录的所有文件
            files.AddRange(dir.GetFiles());

            // 递归遍历所有子目录
            foreach (DirectoryInfo subDir in dir.GetDirectories())
            {
                files.AddRange(GetAllFiles(subDir.FullName));
            }

            return files;
        }
        public LaunchInfo() { }
        public LaunchInfo(LaunchGameInfo launchGameInfo, string userName)
        {
            GameArg = launchGameInfo.GameArg;
            JavaArg = launchGameInfo.JavaArg;
            JavaVersion = launchGameInfo.JavaVersion;
            VersionName = launchGameInfo.VersionName;
            GameDir = launchGameInfo.GameDir;
            AssetsDir = launchGameInfo.AssetsDir;
            AssetsIndexName = launchGameInfo.AssetsIndexName;
            ClientId = launchGameInfo.ClientId;
            NativesDir = launchGameInfo.NativesDir;
            ClassPathDir = launchGameInfo.ClassPathDir;
            PlayerName = userName;
            AuthUuid = "00000FFFFFFFFFFFFFFFFFFFFFF88A2C";
            AuthToken = "00000FFFFFFFFFFFFFFFFFFFFFF88A2C";
            AuthXuid = "00000FFFFFFFFFFFFFFFFFFFFFF88A2C";
            GameAddress = launchGameInfo.GameAddress;
            ClassName = launchGameInfo.ClassName;
        }
        public LaunchInfo(LaunchGameInfo launchGameInfo, AuthResult authResult)
        {
            GameArg = launchGameInfo.GameArg;
            JavaArg = launchGameInfo.JavaArg;
            JavaVersion = launchGameInfo.JavaVersion;
            VersionName = launchGameInfo.VersionName;
            GameDir = launchGameInfo.GameDir;
            AssetsDir = launchGameInfo.AssetsDir;
            AssetsIndexName = launchGameInfo.AssetsIndexName;
            ClientId = launchGameInfo.ClientId;
            NativesDir = launchGameInfo.NativesDir;
            ClassPathDir = launchGameInfo.ClassPathDir;
            PlayerName = authResult.Name;
            AuthUuid = authResult.Uuid;
            AuthToken = authResult.Token;
            AuthXuid = "00000FFFFFFFFFFFFFFFFFFFFFF88A2C";
            GameAddress = launchGameInfo.GameAddress;
            ClassName = launchGameInfo.ClassName;
        }
    }
}
